package com.grj.park.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.grj.park.common.R;
import com.grj.park.domain.Coupons;
import com.grj.park.domain.Customers;
import com.grj.park.service.CouponsService;
import com.grj.park.service.CustomersService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("coupons")
public class CouponsController {

    private final CouponsService couponsService;

    @GetMapping
    public R<List<Coupons>> getCoupons(){
        return R.ok(couponsService.list());
    }

    @PostMapping
    public R<?> saveCoupons(@RequestBody Coupons coupons){
        LambdaQueryWrapper<Coupons> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Coupons::getCouponName, coupons.getCouponName());
        Coupons one = couponsService.getOne(queryWrapper);
        if (one != null){
            return R.fail("优惠券名字已存在");
        }
        couponsService.save(coupons);
        return R.okMsg("添加优惠券成功");
    }
}
